﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenerator.CSharp
{
    /// <summary>
    /// A set of utility functions.
    /// </summary>
    public static class Utilities
    {

		/// <summary>
        /// Parses the identified text into a collection of strings, where each string is no longer 
        /// than the identified maximum length.
        /// </summary>
        public static List<string> ParseText(string text, int maxCharactersPerLine)
        {
            List<string> Lines = new List<string>();
            if (text == null) return Lines;

            //Parse the text into individual lines, where no line is longer than the max lengh. Take 
            //care not to split up a word.
            string RemainingText = text;
            while (RemainingText.Length > maxCharactersPerLine)
            {
                string CurrentLine = RemainingText.Substring(0, maxCharactersPerLine);
                int LastSpaceIndex = CurrentLine.LastIndexOf(" ");
                CurrentLine = CurrentLine.Substring(0, LastSpaceIndex);
                Lines.Add(CurrentLine);
                RemainingText = RemainingText.Remove(0, CurrentLine.Length).TrimStart();
            }
            Lines.Add(RemainingText);

            return Lines;
        }

        /// <summary>
        /// Parses the identified text into a single string with NewLine characters inserted
        /// between each logical line.  Each logical line will be prefaced with the identified padding
        /// string, and each line will not be longer than Settings.MaxCharactersPerLine.
        /// </summary>
        public static string ParseText(string text, string padding)
        {
            CodeBuilder Code = new CodeBuilder();
            int MaxCharactersPerLine = Settings.MaxCharactersPerLine - padding.Length;

            foreach (string ThisLine in ParseText(text,MaxCharactersPerLine))
                Code.Add(padding + ThisLine);

            return Code.ToString().TrimEnd();
        }

    }



}
